package com.frontend;

import java.awt.BorderLayout;

import com.frontend.screens.*;
import com.users.*;
import javax.swing.*;
import java.awt.*;
import java.util.logging.*;
import java.io.IOException;

/**
 * Client application for the Doctors Office program GUI interface which holds
 * and switches between screens.
 * 
 * @author Elizabeth
 * @author Taylor
 */
public class DoctorsOfficeClient extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6965264187177974480L;

	private User currentUser;

	private Screen currentScreen;

	private NetworkInterfacer net;

	private Logger logger;

	/**
	 * Create a new doctors office client to hold important
	 * information about the program
	 */
	public DoctorsOfficeClient() {
		super("The A* Doctor's Office");
		this.setCurrentUser(null);
		this.currentScreen = new LoginScreen(this);
		logger = Logger.getLogger("DocOffLog");
		FileHandler fh;
		try {
			// This block configure the logger with handler and formatter
			fh = new FileHandler("logs\\DoctorsOfficeLogFile.log", true);
			logger.addHandler(fh);
			logger.setLevel(Level.ALL);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		net = null;
		try {
			this.setNet(new NetworkInterfacer(logger));
		} catch (ConnectionException e) {
			logger.log(Level.WARNING, "Initial Connection to Database Failed");
			System.err.println(e.getMessage());
		}
		setMinimumSize(new Dimension(400, 300));

		add(currentScreen, BorderLayout.CENTER);

		setPreferredSize(new Dimension(800, 600));
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void changeScreen(Screen screen) {
		remove(currentScreen);
		currentScreen = screen;
		add(currentScreen);
		currentScreen.revalidate();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
		}

		new DoctorsOfficeClient();
	}

	/**
	 * Sets the network Interfacer
	 * 
	 * @param net network interfacer
	 */
	public void setNet(NetworkInterfacer net) {
		this.net = net;
	}

	/**
	 * gets the network interfacer
	 * 
	 * @return   network interfacer
	 */
	public NetworkInterfacer getNet() {
		return net;
	}

	/**
	 * set the current user in the system
	 * 
	 * @param currentUser
	 */
	public final void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}
	
	/**
	 * Get the user currently logged into the system
	 * 
	 * @return   current user
	 */
	public User getCurrentUser() {
		return currentUser;
	}

	/**
	 * log the current user out of the system
	 */
	public void logout() {
		currentUser = null;
		changeScreen(new LoginScreen(this));
	}

	/**
	 * get the current screen that is displayed
	 * 
	 * @return    the current screen
	 */
	public Screen getCurrentScreen() {
		return currentScreen;
	}
	
	/**
	 * get the logger
	 * 
	 * @return    the logger
	 */
	public Logger getLogger(){
		return logger;
	}

}
